<template>
  <el-form :rules="FormRules" ref="dataForm" :model="Form" label-width="140px" v-loading="listLoading" >
    <el-form-item>
      <div class="el-card_tit">
        <span>基本信息</span>
      </div>
    </el-form-item>
    <el-form-item label="本次验证类型：" prop="loadmode">
      <el-radio-group v-model="Form.loadmode" :disabled="isDisabled">
        <el-radio :label="1">空载</el-radio>
        <el-radio :label="2">满载</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="验证周期：" prop="verifytype">
      <el-radio-group v-model="Form.verifytype" :disabled="isDisabled">
        <el-radio :label="1">冬季</el-radio>
        <el-radio :label="2">夏季</el-radio>
        <el-radio :label="3">使用前</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="选择方案模板：" prop="fanganmuban">
      <el-select v-model="Form.fanganmuban" placeholder="选择方案模板" :disabled="isDisabled">
        <el-option v-for="item in planModelOptions" :key="item.fanganmuban" :label="item.planModelName" :value="item.fanganmuban"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="最长运输时间：" prop="transtime" >
      <el-input-number :disabled="isDisabled" v-model="Form.transtime" :precision="2" :min="0" label="时间"/>&nbsp;min
    </el-form-item>
    <el-form-item label="温度范围：">
      <span>
        <el-input-number v-model="Form.lowtemperature" :precision="2" :step="0.1" label="最低温度：" :disabled="isDisabled"/>&nbsp;℃
      </span>
      <span>到</span>
      <span>
        <el-input-number v-model="Form.hightemperature" :precision="2" :step="0.1" label="最高温度：" :disabled="isDisabled"/>&nbsp;℃
      </span>
    </el-form-item>
    <el-form-item label="温控范围：">
      <span>
        <el-input-number v-model="Form.lowcontroltemperature" :precision="2" :step="0.1" label="最低温度：" :disabled="isDisabled"/>&nbsp;℃
      </span>
      <span>到</span>
      <span>
        <el-input-number v-model="Form.highcontroltemperature" :precision="2" :step="0.1" label="最高温度：" :disabled="isDisabled"/>&nbsp;℃
      </span>
    </el-form-item>
    <el-form-item label="验证参与人员：">
      <el-button type="primary" @click="handleParticipants">查看</el-button>&nbsp;&nbsp;(点击勾选验证参与成员)
    </el-form-item>
    <el-form-item>
      <div class="el-card_tit">
        <span>验证实施</span>
      </div>
    </el-form-item>
     <el-row>
       <el-col :span="12">
         <el-form-item label="预冷温度记录间隔：">
      <el-input-number v-model="Form.precoolinterval" :precision="2" :min="0" label="预冷温度记录间隔" :disabled="isDisabled"/>&nbsp;min
    </el-form-item>
    <el-form-item label="开关门记录间隔：">
      <el-input-number v-model="Form.openinterval" :precision="2" :min="0" label="开关门记录间隔" :disabled="isDisabled"/>&nbsp;min
    </el-form-item>
       </el-col>
       <el-col :span="12">
         <el-form-item label="停机保温记录间隔：">
      <el-input-number v-model="Form.downtimeinterval" :precision="2" :min="0" label="停机保温记录间隔" :disabled="isDisabled"/>&nbsp;min
    </el-form-item>
    <el-form-item label="稳态温度记录间隔：">
      <el-input-number v-model="Form.steadystateinterval" :precision="2" :min="0" label="稳态温度记录间隔" :disabled="isDisabled"/>&nbsp;min
    </el-form-item>
       </el-col>
     </el-row>
    
    
    <el-form-item>
      <div class="el-card_tit">
        <span>验证实施时间</span>
      </div>
    </el-form-item>
    <el-form-item label="计划测试时间：">
      <el-date-picker v-model="Form.testtime" type="datetime" placeholder="请选择时间" :disabled="isDisabled"/>
    </el-form-item>
    <el-form-item>
      <el-button :disabled="checkPermission(['engineer'])? false : true" v-show="saveShow" @click="saveForm(Form)">保存</el-button>
      <el-button :disabled="checkPermission(['engineer'])? false : true" type="primary" @click="submitForm(Form)">提交</el-button>
      <el-button type="warning" @click="generateForm(Form)">生成文档</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import checkPermission from "@/utils/permission"; // 权限判断函数
import { getFanganList } from "@/api/fangan";
import { getTaskData } from "@/api/task";
import { getProjectData ,updateProjectState} from "@/api/project";
import { getGeneratePlan } from "@/api/project";
import { getProjectDataInfo, updateProjectData } from "@/api/projectData";
export default {
  name: "incubatorStep1",
  props: ["getId"],
  data() {
    return {
      planModelOptions: [],
      refrigerantOptions: [],
      customerId: "",
      listLoading: false,
      saveShow: true,
      isDisabled: false,
      Form: {
        loadmode:1,  //验证类型
        verifytype: 1,  //验证周期
        fanganmuban: null, //方案模板
        transtime: 0,  //最长运输时间
        lowtemperature: 0,  //最低温度
        hightemperature: 0,  //最高温度
        lowcontroltemperature:0, //最低温控
        highcontroltemperature:0, //最高温控
        precoolinterval:0,  // 预冷温度记录间隔
        openinterval: 0,  // 开关门记录间隔
        downtimeinterval:0,  // 停机保温记录间隔
        steadystateinterval:0,  // 稳态温度记录间隔
        testtime: new Date() // 计划测试时间
      },
      FormRules: {
        type: [
          { required: true, message: "请选择验证类型", trigger: "change" }
        ],
        verifytype: [
          { required: true, message: "请选择验证周期", trigger: "change" }
        ],
        fanganmuban: [
          { required: true, message: "请选择模板方案", trigger: "change" }
        ],
        transtime: [
          { required: true, message: "请输入最长运输时间", trigger: "blur" }
        ],
        lowtemperature: [
          { required: true, message: "请输入最低温度", trigger: "blur" }
        ],
        hightemperature: [
          { required: true, message: "请输入最高温度", trigger: "blur" }
        ],
        
        coldsource: [
          { required: true, message: "请输入冷源", trigger: "blur" }
        ],
        openInterval: [
          { required: true, message: "请输入开箱记录间隔", trigger: "blur" }
        ],
        recordinterval: [
          { required: true, message: "请输入保温记录间隔", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getPlanModel();
    this.getData();
  },

  methods: {
    checkPermission,
    //获取模板列表
    getPlanModel() {
      //获取主项目customerId
      getProjectData({ projectId: this.getId }).then(res => {
        getTaskData({ taskId: res.data.tid }).then(response => {
          this.customerId = response.data.customerId;
          //获取模板
          getFanganList({ customerId: this.customerId, deviceType: 2 }).then(
            response => {
              this.planModelOptions = response.data.map(item => {
                return { fanganmuban: item.pid, planModelName: item.name };
              });
            }
          );
        });
      });
    },

    //获取子项目进度准备阶段参数
    getData() {
      this.listLoading = true;
      if (checkPermission(["manager"])) {
        this.isDisabled = true;
      }
      getProjectDataInfo({ projectId: this.getId }).then(response => {
        for (const item of response.data) {
          this.Form[item.name] = item.genre == "point" || item.genre == "int" ? Number(item.value) : item.value;
        }
        this.listLoading = false;
      });
    },

    //保存--表格不做验证
    saveForm() {
      let list = [];
      //将form对象转成数组
      for (const key of Object.keys(this.Form)) {
        list.push({ projectId: this.getId, name: key, value: this.Form[key] });
      }
      updateProjectData({ list }).then(response => {
        this.getData();
        this.$notify({
          title: "成功",
          message: "保存成功",
          type: "success",
          duration: 2000
        });
      });
    },
    //提交--表格做验证
    submitForm() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          let list = [];
          //将form对象转成数组
          for (const key of Object.keys(this.Form)) {
            list.push({
              projectId: this.getId,
              name: key,
              value: this.Form[key]
            });
          }
          updateProjectData({ list }).then(response => {
            this.getData();
            this.$notify({
              title: "成功",
              message: "更新成功",
              type: "success",
              duration: 2000
            });
            updateProjectState({ pid: this.getId, state: 32 }).then(_ => {
              this.$emit("changeState")
            });
            this.saveShow = false;
          });
        }
      });
    },
    //生成word文档
    generateForm() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          let list = [];
          //将form对象转成数组
          for (const key of Object.keys(this.Form)) {
            list.push({
              projectId: this.getId,
              name: key,
              value: this.Form[key]
            });
          }
          updateProjectData({ list }).then(response => {
            this.getData();
            updateProjectState({ pid: this.getId, state: 32 }).then(_ => {
              this.$emit("changeState")
            });
            let _this = this;
            _this.$nextTick(() => {
              _this
                .$confirm("确认生成文档吗？", "提示", {
                  type: "warning"
                })
                .then(() => {
                  getGeneratePlan({ projectId: _this.getId }).then(response => {
                    window.open(response.data);
                  });
                });
            });
          });
        }
      });
    },

    handleParticipants() {
      this.$router.push({
        path: "/task/project/progress/participants/" + this.getId
      });
    }
  }
};
</script>
